package all_in_one;

import java.util.Random;
import java.util.concurrent.Semaphore;

public class Aggy extends Thread {
	// Members
	private Semaphore m_sema = new Semaphore(1);
	private CigarettePlace m_place;
	private Random r = new Random();
	public Aggy(CigarettePlace cigarette_place) {
		m_place = cigarette_place;
	}
	
	// Methods
	public void run() {
		while(true) {
			  put();
			  
		  }

	}
	public synchronized void put() {
		int index = r.nextInt(3);
		//System.out.println(String.valueOf(index));
		//System.out.println(m_sema);
		m_sema.acquireUninterruptibly();
		if (index == 0) {
			System.out.println("Aggie put up a paper and match");
			m_place.GetPaperSemaphore().release();
			m_place.GetMatchSemaphore().release();
		}
		else if (index == 1) {
			System.out.println("Aggie put up a tobacco and paper");
			m_place.GetTobaccoSemaphore().release();
			m_place.GetPaperSemaphore().release();
		}
		else {
			System.out.println("Aggie put up a match and tobacco");
			m_place.GetTobaccoSemaphore().release();
			m_place.GetMatchSemaphore().release();
		}
	}
	
	// Getters
	public Semaphore GetSemaphore() {
		return m_sema;
	}
}
